/**
 * Author: grx
 * Description: 学习模块的"学习记录"子功能页面,包含"本周"和"本日"两种统计图表展示
 */
package com.example.parallelandroid;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class StudyStatisticsActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager2 vp2;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study_statistics);

        // 获取控件对象
        getViews();

        // 为ToolBar添加返回键
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("学习记录");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); // 添加默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); // 设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 返回
                Constant.logger.info("finish StudyStatisticsActivity");
            }
        });

        //viewpager2响应滑动效果
        vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //设置标签排列效果
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        // 准备Fragment数据源
        fragments = setData();
        // 实例化适配器对象
        StudyFragmentAdapter adapter = new StudyFragmentAdapter(fragments, this);
        // 绑定适配器
        vp2.setAdapter(adapter);
        // 关联TabLayout和ViewPager2
        TabLayoutMediator mediator = new TabLayoutMediator(
                tabLayout,
                vp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        switch (position) {
                            case 0:// 第一个标签
                                tab.setText("最近七天");
                                tab.setIcon(R.drawable.week);
                                break;
                            case 1: // 第二个标签
                                tab.setText("本日");
                                tab.setIcon(R.drawable.day);
                                break;
                        }
                    }
                }
        );
        mediator.attach();
    }

    // 准备Fragment数据源
    private List<Fragment> setData() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new WeekFragment());
        fragments.add(new DayFragment());
        return fragments;
    }

    // 获取控件对象
    private void getViews() {
        toolbar = findViewById(R.id.toolbar);
        tabLayout = findViewById(R.id.tablayout);
        vp2 = findViewById(R.id.vp2);
    }
}